var server = require('../../utils/server');
var common = require('../../utils/common');
const app = getApp();
Page({
  data: {
    orderId: "",
    orderInfo: [],
    restTime: 15,
    showStorePop: false,
    isShowTip: false,
    qrcodePath: '',
    isShowDetailList: false,
    isPayOk: false,
    serverTel: "",
    showDelPop: false,
    isShowTakeImg: false,
    isShowBack: true,
    showConfirmOrderPop: false,
    isClickCompleting: false,
    curDeliveryCompany: '',
    curDeliveryNumber: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options = {}) {
    this.setData({
      orderId: options.orderId
    })
    this.getOrderDetail();
    this.setData({
      isShowBack: app.globalData.wxversion > 700 || getCurrentPages().length == 1 ? true : false
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},
  getOrderDetail() {
    common.getOrdeDetail(this.data.orderId).then(res => {
      this.setData({
        orderInfo: res.data,
        qrcodePath: common.storePath + 'order/qrcode/code/',
        serverTel: res.data.store.mobile
      })
      this.changeStyle(res.data);
    })
  },
  changeStyle(orderInfo) {
    if (app.globalData.sdkversion > 210 && app.globalData.sdkversion != 1997 && app.globalData.sdkversion != 1991) {
      if (orderInfo.status.code == '99' || orderInfo.status.code == '96') {
        wx.setBackgroundColor({
          backgroundColorTop: '#6b826f' // 顶部窗口的背景色为
        })
        wx.setNavigationBarColor({
          backgroundColor: '#6b826f',
          frontColor: "#ffffff"
        })
      } else if (orderInfo.status.code == '90' || orderInfo.status.code == '95' || orderInfo.status.code == '97' || orderInfo.status.code == '98' || orderInfo.status.code == '80') {
        wx.setBackgroundColor({
          backgroundColorTop: '#36b79f', // 顶部窗口的背景色为
        })
        wx.setNavigationBarColor({
          backgroundColor: '#36b79f',
          frontColor: "#ffffff"
        })
      } else if (orderInfo.status.code < '80') {
        wx.setBackgroundColor({
          backgroundColorTop: '#36D147', // 顶部窗口的背景色为
        })
        wx.setNavigationBarColor({
          backgroundColor: '#36D147',
          frontColor: "#ffffff"
        })
      }
    }
  },
  callServer(e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.tel
    })
  },
  // 取消订单
  cancelOrder(e) {
    var status = e.currentTarget.dataset.status,
      orderid = e.currentTarget.dataset.orderid;
    if (status == "50") {
      this.setData({
        showDelPop: true
      })
    } else {
      wx.redirectTo({
        url: 'orderCancel?orderId=' + orderid,
      })
    }
  },
  confirmDel() {
    this.setData({
      showDelPop: false
    })
    wx.redirectTo({
      url: 'orderCancel?orderId=' + this.data.orderId,
    })
  },
  cancelDel() {
    this.setData({
      showDelPop: false
    })
  },
  orderAgain(e) {
    var orderId = e.currentTarget.dataset.orderid;
    var param = {
      "order_id": orderId,
      "store_id": wx.getStorageSync("storeId"),
      "longitude": app.globalData.longitude,
      "latitude": app.globalData.latitude
    }
    app.globalData.refreshTag = true;
    app.globalData.isAccount = true;
    common.getOrderAgain(param);
  },
  pay(e) {
    var orderid = this.data.orderId
    this.setData({
      isPayOk: true
    })
    var param = {
      "currency": '63',
      "order_id": orderid
    }
    common.pay(param).then(res => {
      if (res.errno == "0") {
        var outtradeno = res.data.out_trade_no;
        //发起支付
        var payParam = res.data;
        var that = this;
        payParam.complete = function (ret3) {
          common.showLoading("支付中...");
          //支付成功
          var isSuc = ret3.errMsg == 'requestPayment:ok';
          wx.reportAnalytics('pay_status', {
            paystatus: isSuc,
            orderid: orderid
          });
          if (isSuc) {
            wx.reportAnalytics('pay_success', {
              paystatus: isSuc,
              orderid: orderid
            });
            server.postJSON(common.storePath + 'order/payrespond/', {
              'out_trade_no': outtradeno
            }, resp => {
              that.setData({
                isPayOk: false
              })
              that.getOrderDetail();

            })
          } else {
            that.setData({
              isPayOk: false
            })
            wx.reportAnalytics('pay_fail', {
              paystatus: isSuc,
              orderid: orderid
            });
            wx.showToast({
              title: '支付失败',
              duration: 2000,
              complete: function () {}
            });
          }
        }
        wx.requestPayment(payParam);
      } else {
        if (res.errmsg == undefined) {
          common.showToast("数据出错啦T_T，请稍候再试 [-3]");
          time = setTimeout(function () {
            wx.hideToast()
          }, 3500)
        } else if (res.errno == "1000000") {
          this.setData({
            showStorePop: true,
            storeStatusInfo: res.data.status_tip
          })
        } else {
          common.showToast(res.errmsg);
        }
      }
    })
  },
  hidePop() {
    this.setData({
      showStorePop: false
    })
  },
  showTip() {
    this.setData({
      isShowTip: !this.data.isShowTip
    })
  },
  previewImage: function (e) {
    this.setData({
      isShowTakeImg: !this.data.isShowTakeImg
    })
  },
  //下拉刷新
  onPullDownRefresh() {
    wx.stopPullDownRefresh();
    common.getOrdeDetail(this.data.orderId).then(res => {
      this.setData({
        orderInfo: res.data,
        qrcodePath: common.storePath + 'order/qrcode/code/'
      })
      this.changeStyle(res.data)
    })
  },
  showListDetail() {
    this.setData({
      isShowDetailList: !this.data.isShowDetailList
    })
  },
  callStore() {
    wx.makePhoneCall({
      phoneNumber: this.data.serverTel
    })
  },
  backPrev() {
    if (getCurrentPages().length == 1) {
      if (this.data.orderInfo.delivery_type != '6') {
        app.globalData.searchOrderStatus = 0;
        wx.switchTab({
          url: '../../pages/order/orderIndex',
        })
      } else {
        wx.switchTab({
          url: '../../pages/index/index',
        })
      }
    } else {
      wx.navigateBack()
    }
  },
  copyOrderId: function (e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.orderid,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制成功'
            })
          }
        })
      }
    })
  },
  showDeliveryNumber(e) {
    this.setData({
      curDeliveryCompany: e.currentTarget.dataset.name,
      curDeliveryNumber: e.currentTarget.dataset.number,
      showDeliveryNumberPop: true
    })
  },
  hideDeliveryNumberPop() {
    this.setData({
      showDeliveryNumberPop: false
    })
  },
  copyDeliveryNumber() {
    var that = this;
    wx.setClipboardData({
      data: this.data.curDeliveryNumber,
      success(res) {
        wx.getClipboardData({
          success(res) {
            that.setData({
              showDeliveryNumberPop: false
            })
          }
        })
      }
    })
  },
  showConfirmOrderPop() {
    this.setData({
      showConfirmOrderPop: true
    })
  },
  hideConfirmOrderPop() {
    this.setData({
      showConfirmOrderPop: false
    })
  },
  confirmOrder() {
    this.setData({
      isClickCompleting: true
    })
    server.postJSON(common.storePath + 'order/complete/', {
      "order_id": this.data.orderId
    }, res => {
      if (res.data.errno == "0") {
        this.setData({
          showConfirmOrderPop: false
        })
        this.getOrderDetail()
        setTimeout(() => {
          this.setData({
            isClickCompleting: false
          })
        }, 1000);

      } else {
        this.setData({
          isClickCompleting: false
        })
        common.showErrorToast(res.data.errmsg);
      }
    })
  }
})